//
//  SettringViewController.m
//  SportsSocial
//
//  Created by yull on 14/10/29.
//  Copyright (c) 2014年 yull. All rights reserved.
//

#import "SettingViewController.h"
#import "LoginRegistNet.h"
#import "IM_Service.h"
#import "SettingSoundCell.h"
#import "AboutViewController.h"

@interface SettingViewController () <UITableViewDataSource,UITableViewDelegate,UIAlertViewDelegate>

@property (nonatomic, strong) UITableView *settingTableView;

@end

@implementation SettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addCommonBackButton];
    
    self.navigationItem.title = @"设置";
    
    CGRect rect = CGRectMake(0, 0, self.view.width, self.view.height - Navibar_Height);
    self.settingTableView = [[UITableView alloc] initWithFrame:rect style:UITableViewStyleGrouped];
    self.settingTableView.delegate = self;
    self.settingTableView.dataSource = self;
    self.settingTableView.backgroundColor = [UIColor clearColor];
    self.settingTableView.separatorInset = UIEdgeInsetsZero;
    if (MORE_THAN_IOS8) {
        [self.settingTableView setLayoutMargins:UIEdgeInsetsZero];
    }
    [self.view addSubview:self.settingTableView];
    
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 20)];
    self.settingTableView.tableHeaderView = headerView;
    
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 40)];
    UIButton *logOutButton = [UIButton buttonWithType:UIButtonTypeCustom];
    logOutButton.backgroundColor = RGB(193, 12, 32);
    logOutButton.layer.cornerRadius = 5;
    [logOutButton setTitle:@"退出登录" forState:UIControlStateNormal];
    [logOutButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    logOutButton.titleLabel.font = [UIFont systemFontOfSize:18.0f];
    logOutButton.frame = CGRectMake(10, 0, self.view.width - 20, footerView.height);
    [footerView addSubview:logOutButton];
    
    self.settingTableView.tableFooterView = footerView;
    
    [logOutButton addTarget:self action:@selector(onClickLogOutButton:) forControlEvents:UIControlEventTouchUpInside];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        SettingSoundCell *soundCell = [tableView dequeueReusableCellWithIdentifier:@"SoundCell"];
        if (!soundCell) {
            soundCell = [[SettingSoundCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"SoundCell"];
            soundCell.selectionStyle = UITableViewCellSelectionStyleNone;
            soundCell.textLabel.text = @"声音提示";
        }
        return soundCell;
    }else {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SettingCell"];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"SettingCell"];
            cell.backgroundColor = [UIColor whiteColor];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            
            if (MORE_THAN_IOS8) {
                [cell setLayoutMargins:UIEdgeInsetsZero];
            }
            cell.textLabel.text = @"关于";
        }
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.row == 1) {
        AboutViewController *aboutViewController = [[AboutViewController alloc] init];
        [self.navigationController pushViewController:aboutViewController animated:YES];
    }
}

#pragma mark - LogOut
- (void)onClickLogOutButton:(id)sender
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"确定退出账号吗?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alert show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        [CurrentUser shareInstance].isLogin = NO;
        
        [self showText:@"正在注销..." time:9999];
        [LoginRegistNet logoutSuccessBlock:^(AFHTTPRequestOperation *operation, id responseObject) {
            
            [self.textProgressHUD hide:YES];
            BOOL success = [[responseObject objectForKey:@"success"] boolValue];
            if (success) {
                
                [[DBManager shareInstance] closeDataBase];
                
                [CurrentUser shareInstance].isLogin = NO;
                
                [[IM_Service instance] disconnectService];
                
                dispatch_async(dispatch_get_main_queue(), ^{
                    [[NSNotificationCenter defaultCenter] postNotificationName:@"UserLogOut" object:nil];
                    
                    [self.navigationController popViewControllerAnimated:YES];
                });
            } else {
                NSString *message = [responseObject objectForKey:@"message"];
                
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self showText:message time:2];
                });
            }
            
        } failBlock:^(AFHTTPRequestOperation *operation, NSError *error) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [self showText:@"网络连接失败" time:2];
            });
        }];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
